<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区域 -->
        <el-card>
            <!-- 头部搜索区域 -->
            <!-- gutter 间隔 -->
            <el-row :gutter='20'>
                <el-col :span='8'>
                    <el-input placeholder="请输入内容" v-model='params.query' clearable @clear='getGoodsList'>
                        <el-button slot="append" icon="el-icon-search" @click='getGoodsList'></el-button>
                    </el-input>
                </el-col>
                <el-col :span='4'>
                    <el-button type='primary' @click='goAddGoods'>添加商品</el-button>
                </el-col>
            </el-row>
            <!-- 商品列表区域 -->
            <el-table :data="goodsList" stripe border>
                <el-table-column type='index'></el-table-column>
                <el-table-column prop="goods_name" label="商品名称"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格(元)" width='110px'></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量" width='95px'></el-table-column>
                <el-table-column prop="add_time" label="创建时间" width='170px'>
                    <template slot-scope='scope'>
                        {{scope.row.add_time | dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width='125px'>
                    <template slot-scope='scope'>
                        <!-- 编辑按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size='mini'
                            @click='editGoods(scope.row.goods_id)'></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete" size='mini'
                            @click='removeGoods(scope.row.goods_id)'></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="params.pagenum" :page-sizes="[10, 20, 50]" :page-size="params.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" background>
            </el-pagination>
        </el-card>
        <!-- 编辑商品弹框 -->
        <el-dialog title="编辑商品" :visible.sync="editDialogVisible" width="50%">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
                <el-form-item label="商品名称" prop="goods_name">
                    <el-input v-model="editForm.goods_name"></el-input>
                </el-form-item>
                <el-form-item label="商品价格" prop="goods_price">
                    <el-input v-model="editForm.goods_price"></el-input>
                </el-form-item>
                <el-form-item label="商品数量" prop="goods_number">
                    <el-input v-model="editForm.goods_number"></el-input>
                </el-form-item>
                <el-form-item label="商品重量" prop="goods_weight">
                    <el-input v-model="editForm.goods_weight"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editFormVisible">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                // 商品列表
                goodsList: [],
                // 查询参数对象
                params: {
                    query: '',
                    pagenum: 1,
                    pagesize: 10
                },
                total: 0,
                editDialogVisible: false,
                editForm: {
                    goods_name: '',
                    goods_price: 0,
                    goods_number: 0,
                    goods_weight: 0,
                    goods_id: 0
                },
                editFormRules: {
                    goods_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
                    goods_price: [{ required: true, message: '请输入商品价格', trigger: 'blur' },],
                    goods_number: [{ required: true, message: '请输入商品数量', trigger: 'blur' },],
                    goods_weight: [{ required: true, message: '请输入商品重量', trigger: 'blur' },]
                }
            }
        },
        created() {
            this.getGoodsList();
        },
        methods: {
            // 获取商品列表数据
            async getGoodsList() {
                const { data: res } = await this.$axios.get('goods', { params: this.params });
                if (res.meta.status !== 200) return this.$message.error('获取商品列表失败!');
                // console.log(res.data);
                this.goodsList = res.data.goods;
                this.total = res.data.total;
            },
            // 监听 pagesize 改变的事件
            handleSizeChange(newSize) {
                this.params.pagesize = newSize;
                this.getGoodsList();
            },
            // 监听 页码值 改变的事件
            handleCurrentChange(Current) {
                this.params.pagenum = Current;
                this.getGoodsList();
            },
            // 删除商品
            async removeGoods(id) {
                const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err => err);
                if (confirmResult !== 'confirm') return this.$message.info('已取消删除!');
                const { data: res } = await this.$axios.delete(`goods/${ id }`);
                if (res.meta.status !== 200) return this.$message.error('删除失败!');
                this.$message.success('删除成功!');
                this.getGoodsList();
            },
            // 点击添加商品按钮跳转到添加商品页面
            goAddGoods() {
                this.$router.push('/goods/add');
            },
            // 点击编辑按钮查询商品
            async editGoods(id) {
                const { data: res } = await this.$axios.get(`goods/${ id }`);
                if (res.meta.status !== 200) return this.$message.error('查询商品失败!');
                this.editForm = res.data;
                this.editDialogVisible = true;
            },
            // 发起请求编辑商品
            editFormVisible() {
                this.$refs.editFormRef.validate(async valid => {
                    if (!valid) return;
                    const { data: res } = await this.$axios.put(`goods/${ this.editForm.goods_id }`, this.editForm);
                    if (res.meta.status !== 200) return this.$message.error('修改失败!');
                    this.$message.success('修改成功!');
                    // console.log(res);
                    this.getGoodsList();
                    this.editDialogVisible = false;
                })
            }
        }
    }
</script>

<style scoped>

</style>